﻿window.todoApp.todoListViewModel = (function (ko, datacontext) {
	/// <field name="todoLists" value="[new datacontext.todoList()]"></field>
	var todoLists = ko.observableArray(),
		selectedVehicle = ko.observable(),
		error = ko.observable(),
		refreshTimeIntervals = ko.observableArray([30, 60, 120]),
		selectedInterval = ko.observable(),
		addTodoList = function () {
			var todoList = datacontext.createTodoList();
			todoList.isEditingListTitle(true);
			datacontext.saveNewTodoList(todoList)
				.then(addSucceeded)
				.fail(addFailed);

			function addSucceeded() {
				showTodoList(todoList);
			}
			function addFailed() {
				error("Save of new todoList failed");
			}
		},
		showTodoList = function (todoList) {
			todoLists.unshift(todoList); // Insert new todoList at the front
		},
		deleteTodoList = function (todoList) {
			todoLists.remove(todoList);
			datacontext.deleteTodoList(todoList)
				.fail(deleteFailed);

			function deleteFailed() {
				showTodoList(todoList); // re-show the restored list
			}
		},
		currentLocation = {
			Latitude: 0.0,
			Longitude: 0.0
		},
	timer,
	findVehicleLocation = function () {
		var vehicleLocation = ko.observable();
		error("Fetching location for vehicle.");
		if (!selectedVehicle()) {
			error("Please select a vehicle.");
			return;
		}

		datacontext.getVehicleLocation(selectedVehicle().VehicleId, vehicleLocation, error);
		var subscription = vehicleLocation.subscribe(function (item) {
			//alert(item[0].Latitude);

			if (item.length > 0 && currentLocation.Latitude != item[0].Latitude && currentLocation.Longitude != item[0].Longitude) {
				var ordinates = new google.maps.LatLng(item[0].Latitude, item[0].Longitude);
				var mapOptions = {
					zoom: 8,
					center: ordinates,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				var map = new google.maps.Map($('#map-canvas')[0], mapOptions);
				var marker = new google.maps.Marker({
					position: ordinates,
					map: map,
					title: "Vehicle : " + item[0].VehicleId
				});

				currentLocation.Latitude = item[0].Latitude;
				currentLocation.Longitude = item[0].Longitude;
				clearInterval(timer);
				timer = setInterval(findVehicleLocation, selectedInterval() * 1000);
				error("");
			} else {
				error("");
			}
			subscription.dispose();
		});


	};

	datacontext.getVehicleLists(todoLists, error); // load todoLists

	return {
		todoLists: todoLists,
		error: error,
		addTodoList: addTodoList,
		deleteTodoList: deleteTodoList,
		selectedVehicle: selectedVehicle,
		refreshTimeIntervals: refreshTimeIntervals,
		selectedInterval: selectedInterval,
		findVehicleLocation: findVehicleLocation
	};

})(ko, todoApp.datacontext);


gMapsCallback = function () {
	$('#btntrack').removeAttr('disabled');
};

$(document).ready(function () {
	var mapscript = document.createElement("script");
	mapscript.type = "text/javascript";
	mapscript.src = "http://maps.google.com/maps/api/js?v=3.exp&key=AIzaSyA-7hsY7kHkJGA4FlogrFp-ZBsQ9-dXlUk" + "&callback=gMapsCallback";
	$('head').append(mapscript);
});

// Initiate the Knockout bindings
ko.applyBindings(window.todoApp.todoListViewModel);
